package cn.zcib.film.service.impl;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Play;
import cn.zcib.film.bean.vo.PlayVO;
import cn.zcib.film.mapper.PlayMapper;
import cn.zcib.film.service.PlayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class PlayServiceImpl implements PlayService {
    @Autowired
    private PlayMapper playMapper;
    @Override
    public List<Play> findPlayList(PlayVO vo) {
        //结束日期需要指定为当天23点59分59秒
        if (vo.getEndDate()!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                //设置正确的结束日期
            //1.通过sdf获取格式化日期字符串
            String format = sdf.format(vo.getEndDate());
            //2.获取Calendar对象
            Calendar c = Calendar.getInstance();
            try {
                //3.设置c的时间值
                c.setTime(sdf.parse(format));
                //4.对c的时间进行计算
            c.add(Calendar.DATE, 1);  // number of days to add
                //5.获取+1后的时间
            format = sdf.format(c.getTime());  // format is now the new date
                //6.将时间重新赋值给endDate
            vo.setEndDate(sdf.parse(format));
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
        return playMapper.getPlayList(vo);
    }

    @Override
    public int cancelPlay(Integer playId) {
        return playMapper.updatePlayById(playId);
    }
}
